import csv

# Count()
def calculate_bmr(gender, height, age, weight):
    if gender == '男':
        bmr = 66 + (13.7 * weight) + (5.0 * height) - (6.8 * age)
    elif gender == '女':
        bmr =655 + (9.6 * weight) + (1.8 * height) - (4.7 * age)
    else:
        print("暂时不支持该性别的BMR计算")
    return bmr

# Save()
def save_data_to_csv(csv_file_path, data):
    with open(csv_file_path, 'a', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(data)

# Menu()函数
def menu():
    print("欢迎使用BMR计算器!")
    print("请选择一个选项")
    print("1.计算BMR")
    print("2. 退出")
    print("3. 保存 CSV ")

# Main()
def main():
    csv_file_path = 'bmr_history.csv'
    while True:
        menu()
        choice = input("输入你的选择 (1-3): ")
        if choice == '1':
            gender = input("输入你的性别 (男/女): ")
            height = float(input("输入你的身高(cm): "))
            age = int(input("输入你的年龄: "))
            weight = float(input("输入你的体重 (kg): "))
            try:
                bmr = calculate_bmr(gender, height, age, weight)
                print(f"Your BMR is: {bmr:.2f}")
                save_data_to_csv(csv_file_path, [gender, height, age, weight, bmr])
            except ValueError as e:
                print(e)
        elif choice == '2':
            print("感谢您使用BMR计算器，再见!")
            break
        elif choice == '3':
            with open(csv_file_path, 'w', newline='') as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(["Gender", "Height (cm)", "Age", "Weight (kg)", "BMR"])
            print("CSV文件头已保存.")
        else:
            print("无效选择，请重试！.")

if __name__ == '__main__':
    main()